﻿using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace zijian666.Converts.Core;

/// <summary>
/// 描述字符串资源
/// </summary>
public class ResourceStrings
{
    /// <summary>
    /// 提供一种通过关键字字符串查询资源的方式
    /// </summary>
    private readonly IReadOnlyDictionary<string, string> _dict;

    /// <summary>
    /// 初始化属性与值的键值对
    /// </summary>
    public ResourceStrings()
    {
        var dict = new Dictionary<string, string>();
        foreach (var p in this.GetType().GetProperties())
        {
            if (p.PropertyType == typeof(string) && p.CanRead && p.GetIndexParameters().Length == 0)
            {
                dict[p.Name] = (string)p.GetValue(this);
            }
        }
        _dict = new ReadOnlyDictionary<string, string>(dict);
    }

    /// <summary>
    /// 通过关键字获取字符串资源，如果没有找到，返回 <paramref name="key"/>
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public string this[string key] => _dict.TryGetValue(key, out var value) ? value : key;

    /// <summary>
    /// `{0}`({1}) 无法转换为 {2}
    /// </summary>
    public virtual string CANT_CONVERT { get; set; } = "`{0}`({1}) 无法转换为 {2}";

    /// <summary>
    /// 无法生成 {0} 类型的转换器：{1}
    /// </summary>
    public virtual string CANT_BUILD_CONVERTOR { get; set; } = "无法生成 {0} 类型的转换器：{1}";
    /// <summary>
    /// 转换器{0} 转换失败：{1}
    /// </summary>
    public virtual string CONVERTOR_CAST_FAIL { get; set; } = "转换失败：{1}；(转换器：{0})";

    /// <summary>
    /// 属性：{0}.{1} 转换失败
    /// </summary>
    public virtual string PROPERTY_CAST_FAIL { get; set; } = "{0} 属性名 转换失败: {1}";

    /// <summary>
    /// 属性：{0}.{1} 设置失败，值：{2}
    /// </summary>
    public virtual string PROPERTY_SET_FAIL { get; set; } = "{0} 属性 {1} 设置失败，值：{2}";

    /// <summary>
    /// {0} 无法创建对象实例
    /// </summary>
    public virtual string INSTANTIATION_FAIL { get; set; } = "{0} 无法创建对象实例";

    /// <summary>
    /// 值超过限制：{0}
    /// </summary>
    public string VALUE_OVERFLOW { get; set; } = "值超过限制：{0}";

    /// <summary>
    /// 集合插值失败，{0}[{1}]={2}
    /// </summary>
    public virtual string COLLECTION_ADD_FAIL { get; set; } = "集合 {0} 插值失败，[{1}]={2}";

    /// <summary>
    /// 集合键转换失败：{0}，KEY={1}
    /// </summary>
    public virtual string COLLECTION_KEY_FAIL { get; set; } = "集合 {0} 键 {1} 转换失败";

    /// <summary>
    /// 泛型定义类型
    /// </summary>
    public virtual string CLASS_IS_GENERIC_TYPE_DEFINITION { get; set; } = "泛型定义类型";

    /// <summary>
    /// 静态类型
    /// </summary>
    public virtual string CLASS_IS_STATIC { get; set; } = "静态类型";

    /// <summary>
    /// 未找到合适的转换器
    /// </summary>
    public virtual string CONVERTOR_NOT_FOUND { get; set; } = "未找到合适的转换器";
}
